package pl.edu.pw.polygen.ui.download;

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.beans.FileBean;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

@Component("downloadForm")
@Scope("prototype")
public class TriangleFormImpl extends GridLayout implements TriangleForm{

	private static final long serialVersionUID = 6113314257231671872L;

	private static final String DIAGRAM_TYPE_NAME_PROPERTY = "name";
	private static final String DIAGRAM_TYPE_OPTION_PROPERTY = "option";
	private static final String COMMON_FIELD_WIDTH = "40em";
	
	private static final int rowsAmount = 4;
	private static final int columnsAmount = 2;
	
	private FileBean fileBean;
	
	private Label projectName;
	private Label fileName;
	private ComboBox diagramTypeCombo;
	private TextField qualityMeshField;
	
	public TriangleFormImpl(){
		super(columnsAmount,rowsAmount);
		setSpacing(true);
	}
	
	@SuppressWarnings("unused")
	@PostConstruct
	private void initialize(){
		addComponent(new Label("Project: "), 0, 0);
		addComponent(getProjectNameField(),1,0);
		addComponent(new Label("Name: "), 0, 1);
		addComponent(getFileNameField(),1,1);
		addComponent(new Label("Type: "),0,2);
		addComponent(getDiagramTypeCombo(),1,2);
		addComponent(new Label("Mesh quality "),0,3);
		addComponent(getQualityMeshField(),1,3);
	}

	@Override
	public void setFileBean(FileBean fileBean) {
		this.fileBean = fileBean;
		this.setProjectName(this.fileBean.getProject().getName());
		this.setFileName(this.fileBean.getFileName());
	}
	
	private Label getProjectNameField(){
		if(projectName==null){
			projectName = new Label();
			projectName.setWidth(COMMON_FIELD_WIDTH);
		}
		return projectName;
	}
	

	private Label getFileNameField(){
		if(fileName==null){
			fileName = new Label();
			fileName.setWidth(COMMON_FIELD_WIDTH);
		}
		return fileName;
	}
	
	private ComboBox getDiagramTypeCombo() {
		if(diagramTypeCombo == null){
			BeanItemContainer<DiagramType> diagramTypeContainer = new BeanItemContainer<DiagramType>(DiagramType.class);
			diagramTypeContainer.addBean(new DiagramType("-D", "Delanuay triangulation"));
			diagramTypeContainer.addBean(new DiagramType("-v", "Voronoi diagram"));
			diagramTypeCombo = new ComboBox();
			diagramTypeCombo.setContainerDataSource(diagramTypeContainer);
			diagramTypeCombo.setRequired(true);
			diagramTypeCombo.setRequiredError("Please select an operation");
			diagramTypeCombo.setImmediate(true);
			diagramTypeCombo.setItemCaptionPropertyId(DIAGRAM_TYPE_NAME_PROPERTY);
			diagramTypeCombo.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
			diagramTypeCombo.setWidth(COMMON_FIELD_WIDTH);
		}
		return diagramTypeCombo;
		
	}
	
	private TextField getQualityMeshField() {
		if(qualityMeshField == null){
			qualityMeshField = new TextField();
			qualityMeshField.setValue("20");
		}
		return qualityMeshField;
	}
	
	
	@Override
	public String getProjectName() {
		return (String) projectName.getValue();
	}

	@Override
	public void setProjectName(String projectName) {
		this.projectName.setValue(projectName);
	}

	@Override
	public String getFileName() {
		return (String) fileName.getValue();
	}

	@Override
	public void setFileName(String name) {
		fileName.setValue(name);
	}

	@Override
	public boolean isValid() {
		return diagramTypeCombo.isValid();
	}
	
	@Override
	public String getDiagramType() {
		DiagramType diagramType = (DiagramType) diagramTypeCombo.getValue();
		return diagramType.getOption();
	}
	
	@Override
	public double getMeshQuality() throws NumberFormatException {
		String meshString = (String) qualityMeshField.getValue();
		return Double.parseDouble(meshString);
	}

	public class DiagramType{
		private String option;
		private String name;
		
		public DiagramType(String option, String name) {
			this.option = option;
			this.name = name;
		}

		public String getOption() {
			return option;
		}
		
		public void setOption(String option) {
			this.option = option;
		}
		
		public String getName() {
			return name;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		
	}
	
	
}
